﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.socket;
using System.Threading;
using mars_server.csharp.Util;
using mars_server.csharp.controller;
using mars_server.csharp.CO;
using mars_server.csharp.cache;

namespace mars_server.csharp.myTask
{
    public class BusinessTask
    {
        private BusinessTask() { }
        private static readonly BusinessTask instance = new BusinessTask();
        public static BusinessTask Instance
        {
           get
           {
               return instance;
           }
        }

        DispatcherController dispatcherController = DispatcherController.Instance;

        public void dealMessage(UserSocket userSocket)
        {
            Task.Run(() =>
            {
                ThreadCountTool.addThreadNum();
                Console.WriteLine("[info]|--当前线程数："+ ThreadCountTool.coutThreadNum());
                Console.WriteLine(" ");
                Console.WriteLine("+++++++++++++++++【业务线程开启】++++++++++++++++++");
                Console.WriteLine("[info]:--【业务线程】---" + SocketUtil.getSocketInfo(userSocket) + " 开启业务线程");
                List<MessageCO> messageCOs = JsonUtil.JsonByteToMessageCO(SocketUtil.getBytesFromUs(userSocket));
                if(messageCOs != null)
                {
                    foreach (MessageCO messageCO in messageCOs)
                    {
                        dispatcherController.dispacther(messageCO, userSocket);
                    }
                }
                userSocket.IsOp = false;
                Console.WriteLine("[info]:--【业务线程】---" + SocketUtil.getSocketInfo(userSocket) + "业务线程结束");
                Console.WriteLine("+++++++++++++++++【业务线程关闭】++++++++++++++++++");
                Console.WriteLine(" ");
                ThreadCountTool.subThreadNum();
                Console.WriteLine("[info]|--当前线程数：" + ThreadCountTool.coutThreadNum());
            });
            
        } 
    }
}
